	
	// 重写easyUI验证框的验证方法
	$.extend($.fn.validatebox.defaults.rules, {    
	    ISBMValidate: {    
	        validator: function(value, param){    
	            return parseInt(value) ? value.length == 10 || value.length == 13 : false;    
	        },    
	        message: 'ISBM编码必须是10位或者13位的数字'   
	    }    
	});  

	// base info 基本信息
	var BI = {
		// 图书信息
		book: {
			id 	 : '#book-dg',
			name : 'book',
			url  : {query: 'list', save: 'save', del: 'delBatch'},
			dlg	 : {titleInsert : '新书登记', titleUpdate: '图书信息修改',
					insertRule: function(){
						$('#publishDate').css('display', 'block');
						$('#ISBN').textbox('readonly', false);
						$('#booksNum').css('display', 'block');
					},
					updateRule: function(){
						$('#publishDate').css('display', 'none');
						$('#ISBN').textbox('readonly', true);
						$('#booksNum').css('display', 'none');
					}},
			toolbar : {id: '#book-tb'},
		},
		// 图书子表信息
		sub: {
			id	 : [],
			url  : {query: 'detailList', insert: '', update: 'updateDetail', del: 'delDetail'},
			objs : [], // 子表格数组
		},
		// 出版社信息
		publisher: {
			id 	 : '#publisher-dg',
			name : 'publisher',
			url  : {query: 'publisher/list', insert: 'publisher/add', update: 'publisher/modify', del: 'publisher/del'},
			dlg	 : {title : '出版社信息'},
			toolbar : {id: '#publisher-tb'},
			combobox: {
				url : 'publisher/combox', // 下拉框获取数据url
				p 	: {id: '#publisherId'},
				s_p : {id: '#s_publisherId'}
			}
		},
		// 类型信息
		category: {
			id	 : '#category-dg',
			name : 'category',
			url  : {query: 'bookCategory/list', insert: 'bookCategory/add', update: 'bookCategory/modify', del: 'bookCategory/del'},
			dlg	 : {title : '图书类型信息'},
			toolbar : {id: '#category-tb'},
			combobox: {
				url : 'bookCategory/combox', // 下拉框获取数据url
				c 	: {id: '#categoryId'},
				s_c : {id: '#s_categoryId'}
			}
		},
	};
	// 图书表格对象
	BI.book.obj 	 = new easyUI.extend.TABLE(BI.book.name, BI.book.url);
	// 出版社弹框表格对象
	BI.publisher.obj = new easyUI.extend.DLAG_TABLE(BI.publisher.dlg.title, BI.publisher.name, BI.publisher.url);
	// 出版社下拉框对象
	BI.publisher.combobox.p.obj = new easyUI.extend.Combobox(BI.publisher.combobox.p.id, BI.publisher.combobox.url);
	BI.publisher.combobox.s_p.obj = new easyUI.extend.Combobox(BI.publisher.combobox.s_p.id, BI.publisher.combobox.url);
	// 类型弹出框表格对象
	BI.category.obj  = new easyUI.extend.DLAG_TABLE(BI.category.dlg.title, BI.category.name, BI.category.url);
	// 类型下拉抗对象
	BI.category.combobox.c.obj = new easyUI.extend.Combobox(BI.category.combobox.c.id, BI.category.combobox.url);
	BI.category.combobox.s_c.obj = new easyUI.extend.Combobox(BI.category.combobox.s_c.id, BI.category.combobox.url);

	// 下拉框初始化
	function comboboxInit(){
		BI.publisher.combobox.p.obj.init();
		BI.publisher.combobox.s_p.obj.init();
		BI.category.combobox.c.obj.init();
		BI.category.combobox.s_c.obj.init();
	}
	// 重新加载出版社下拉框
	function publisherComboxReload(){
		BI.publisher.combobox.p.obj.reload();
		BI.publisher.combobox.s_p.obj.reload();
	}
	// 重新加载类型下拉框
	function categoryComboxReload(){
		BI.category.combobox.c.obj.reload();
		BI.category.combobox.s_c.obj.reload();
	}
	
	$(document).ready(function(){
			
			// 初始化datagrid
			$(BI.book.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'Id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'Id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 465, //高度
		        url: BI.book.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
				resizable: false,
				rowStyler:function(index,row){   
			        if (row.status == -1){   
		            		return 'background-color:#ccc;color:#878787;';   
			        	}   
			   		},
//				loadFilter: function(data){
//	        		if(!data.rows){
//	        			data.rows = [];
//	        		}
//	        		return data;
//				},
		        columns: [
		            [
		                { field: 'id', title:'编号', align:'center', width: 70, hidden: true},
		                { field: 'name', title:'书名', align:'center', width: 70},
		                { field: 'author', title:'作者', align:'center', width: 70},
		                { field: 'publishDate', title:'出版日期', align:'center', width: 70},
		                { field: 'publisherId', title:'出版社id', align:'center', width: 70, hidden: true},
		                { field: 'publisherName', title:'出版社', align:'center', width: 70},
		                { field: 'categoryId', title:'图书类型', align:'center', width: 70, hidden: true},
		                { field: 'categoryName', title:'类型', align:'center', width: 70},
		                { field: 'ISBN', title:'书码', align:'center', width: 70},
		                { field: 'price', title:'价格', align:'right', width: 70, formatter:
							function(value, row, index){
								return "￥" + value;
							}},
						{ field: 'operation', title:'操作', align:'center', width: 70, formatter:
							function(value, row, index){
								if (row.status != -1){
									return '<a class="editor-btn green" href="javaScript:void(0);" onclick="BI.book.obj.update(BI.book.dlg.titleUpdate, BI.book.dlg.updateRule)">修改</a><a class="editor-btn red" href="javaScript:BI.book.obj.del(\'您确定要注销《' + row.name + '》-ISBM:' + row.ISBN +'这类书籍吗？\')">注销</a>';
								} else {
									return '<span class="span-gray">已注销</span>';
								}}},
		            ]
		        ],
		    	view: detailview,
		    	detailFormatter: function(index,row){
		    		return '<div><table id="sub_' + index + '"></table></div>';
		    	},
		    	onLoadSuccess: function(data){
		    		if (parseInt(sessionStorage.getItem("authority")) === 0){
		    			$(BI.book.id).datagrid('hideColumn', 'operation');
		    		}W
		    	},
		    	onExpandRow: function(index,row){
		    		row.editing = false;
		    		// 单元格编辑标识
		    		easyUI.extend.TABLE_CELL.prototype.sign = false;
		    		// 子表格Id
		    		BI.sub.id[index] = '#sub_' + index;
		    		// 图书子表格对象
					BI.sub.objs[index] = new easyUI.extend.TABLE_SUB(BI.sub.id[index], BI.sub.url);
		    		var ddv = $(this).datagrid('getRowDetail',index).find(BI.sub.id[index]);
		    		var tableIndex = index;
		    		
		    		ddv.datagrid({
		    			url: BI.sub.url.query,
		    			width: '80%',
		    			align: 'center',
		    			fitColumns: true,
		    			singleSelect: true,
		    			rownumbers: true,
		    			loadMsg: '努力加载中。。。。',
		    			height: 'auto',
		    			resizable: false,
		    			rowStyler:function(index,row){   
					        if (row.status == -1){   
				            		return 'background-color:#ccc;color:#878787;';   
					        	}   
					   		},
		    			queryParams: {publishDate: row.publishDate, ISBN: row.ISBN},
		    			columns:[[
									{ field: 'id', title:'编号', align:'center', width: 70},
									{ field: 'name', title:'书名', align:'center', width: 70},
									{ field: 'status', title:'图书状态', align:'center', width: 70, formatter: 
										function(value,row,index){
											if (value == 0){
												return "外借";
											} else if (value == 1){
												return "在库";
											} else if (value == -1){
												return "注销";
										}},
										editor:{
											type:'combobox',
											options:{
												editable: false,
												valueField:'value',
												textField:'name',
												data:[{value:1, name:'在库'}, {value:0, name:'外借'}],
												required:true
											}
										}},
									{ field: 'operation', title:'操作', align:'center', width: 70, formatter:
										function(value, row, index){
	
											if (row.status != -1){
												if (row.editing){
													var s = '<a class="editor-btn green" href="javaScript:BI.sub.objs[' + tableIndex + '].update()" >保存</a>';
													var c = '<a class="editor-btn red" href="javaScript:BI.sub.objs[' + tableIndex + '].tableCell.cancel()" >取消</a>';
													return s + c;
												} else {
													var m = '<a class="editor-btn green" href="javaScript:BI.sub.objs[' + tableIndex + '].tableCell.editor()" >修改</a>';
													var l = '<a class="editor-btn red" href="javaScript:BI.sub.objs[' + tableIndex + '].del(\'您确定要删除《' + row.name + '》这本书吗？\')" >注销</a>';
													return m + l;
												}
											} else {
												return '<span class="span-gray">已注销</span>';
											}}},
		    			]],
		    			onBeforeEdit:function(index,row){
		    				row.editing = true;
		    				easyUI.extend.TABLE_CELL.prototype.sign = true;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onAfterEdit:function(index,row){
		    				row.editing = false;
		    				easyUI.extend.TABLE_CELL.prototype.sign = false;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onCancelEdit:function(index,row){
		    				row.editing = false;
		    				easyUI.extend.TABLE_CELL.prototype.sign = false;
		    				BI.sub.objs[tableIndex].tableCell.updateActions(index);
		    			},
		    			
		    			onResize:function(){
		    				$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    			},
		    			onLoadSuccess:function(){
		    				setTimeout(function(){
		    					$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    				},0);
		    				if (parseInt(sessionStorage.getItem("authority")) === 0){
				    			$(this).datagrid('hideColumn', 'operation');
				    		}
		    			},
		    		});
		    		
		    		$(BI.book.id).datagrid('fixDetailRowHeight',index);
		    		
//		    		// 表格行样式
//		    		$(BI.sub.id[tableIndex]).datagrid({ 
//						rowStyler:function(index,row){   
//					        if (row.status == -1){   
//				            		return 'background-color:#ccc;color:#878787;';   
//					        	}   
//					   		}   
//						});
		    		
		    	},
		               
		        toolbar: $(BI.book.toolbar.id),
		    });
		    
		    // 出版社表格
		    $(BI.publisher.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'Id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'Id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 300, //高度
		        url: BI.publisher.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
		        loadFilter: function(data){
		        		if(!data.rows){
		        			data.rows = [];
		        		}
		        		return data;
		    	},

		        columns: [
		            [
		                { field: 'id', title:'编号', align:'center', width: 100, hidden: true},
		                { field: 'name', title:'出版社', align:'center', width: 70, editor: {type: 'validatebox', options: {required: true}}},
						{ field: 'operation', title:'操作', align:'center', width: 70, formatter:
							function(value, row, index){
								if (row.editing){
									if (value != -1){
										var s = '<a class="editor-btn green" href="javaScript:BI.publisher.obj.updateSave()" >保存</a>';
										var c = '<a class="editor-btn red" href="javaScript:BI.publisher.obj.tableCell.cancel()" >取消</a>';
										return s + c;
									} else {
										var s = '<a class="editor-btn green" href="javaScript:BI.publisher.obj.insertSave()" >保存</a>';
										var c = '<a class="editor-btn red" href="javaScript:BI.publisher.obj.insertCancel()" >取消</a>';
										return s + c;
									}
								} else {
									var m = '<a class="editor-btn green" href="javaScript:BI.publisher.obj.tableCell.editor()" >编辑</a>';
									var l = '<a class="editor-btn red" href="javaScript:BI.publisher.obj.del()" >删除</a>';
									return m + l;
								}}},
		            ]
		        ],
		        onBeforeEdit:function(index,row){
    				row.editing = true;
    				easyUI.extend.TABLE_CELL.prototype.sign = true;
    				BI.publisher.obj.tableCell.updateActions(index);
    			},
		    			
    			onAfterEdit:function(index,row){
    				row.editing = false;
    				easyUI.extend.TABLE_CELL.prototype.sign = false;
    				row.operation = 0;
    				BI.publisher.obj.tableCell.updateActions(index);
    				// 重新加载出版社组合框
    				publisherComboxReload();
    			},
    			
    			onCancelEdit:function(index,row){
    				row.editing = false;
    				easyUI.extend.TABLE_CELL.prototype.sign = false;
    				row.operation = 0;
    				BI.publisher.obj.tableCell.updateActions(index);
    			},
        
		        toolbar: $(BI.publisher.toolbar.id),
		    });
		   
		   // 类型表格
		   $(BI.category.id).datagrid({ 
		        striped: true,
		        rownumbers: true,
		        pageSize: 10,
		        idField: 'Id', //这个idField必须指定为输出的id，输出的是Id就必须是Id，不能小写
		        sortName: 'Id',
		        sortOrder: 'desc',
		        width: '100%', //宽度
		        height: 300, //高度
		        url: BI.category.url.query, //请求路径
		        singleSelect: true, //单选
		        fitColumns: true, //设置为true将自动使列适应表格宽度以防止出现水平滚动。
		        pagination: true, //启用分页
		        loadFilter: function(data){
	        		if(!data.rows){
	        			data.rows = [];
	        		}
	        		return data;
		        },
		        
		        columns: [
		            [
		                { field: 'id', title:'编号', align:'center', width: 100, hidden: true},
		                { field: 'name', title:'类型', align:'center', width: 70, editor: {type: 'validatebox', options: {required: true}}},
						{ field: 'operation', title:'操作', align:'center', width: 70, formatter:
							function(value, row, index){
								if (row.editing){
									if (value != -1){
										var s = '<a class="editor-btn green" href="javaScript:BI.category.obj.updateSave()" >保存</a>';
										var c = '<a class="editor-btn red" href="javaScript:BI.category.obj.tableCell.cancel()" >取消</a>';
										return s + c;
									} else {
										var s = '<a class="editor-btn green" href="javaScript:BI.category.obj.insertSave()" >保存</a>';
										var c = '<a class="editor-btn red" href="javaScript:BI.category.obj.insertCancel()" >取消</a>';
										return s + c;
									}
								} else {
									var m = '<a class="editor-btn green" href="javaScript:BI.category.obj.tableCell.editor()" >编辑</a>';
									var l = '<a class="editor-btn red" href="javaScript:BI.category.obj.del()" >删除</a>';
									return m + l;
								}}},
		            ]
		        ],
		        onBeforeEdit:function(index,row){
    				row.editing = true;
    				easyUI.extend.TABLE_CELL.prototype.sign = true;
    				BI.category.obj.tableCell.updateActions(index);
    			},
		    			
    			onAfterEdit:function(index,row){
    				row.editing = false;
    				row.operation = 0;
    				easyUI.extend.TABLE_CELL.prototype.sign = false;
    				BI.category.obj.tableCell.updateActions(index);
    				// 重新加载类型组合框
    				categoryComboxReload();
    			},
    			
    			onCancelEdit:function(index,row){
    				row.editing = false;
    				row.operation = 0;
    				easyUI.extend.TABLE_CELL.prototype.sign = false;
    				BI.category.obj.tableCell.updateActions(index);
    			},
		        
		        toolbar: $(BI.category.toolbar.id),
		    });
			
//			// 表格行样式
//			$(BI.book.id).datagrid({ 
//				rowStyler:function(index,row){   
//			        if (row.status == -1){   
//		            		return 'background-color:#ccc;color:#878787;';   
//			        	}   
//			   		}   
//			}); 
			
			// 组合框初始化
			comboboxInit();
			
	});
	
	
	
